package com.qidian.mes.sys.service.impl;

import com.qidian.mes.pojo.Dict;
import com.qidian.mes.sys.mapper.DictMapper;
import com.qidian.mes.sys.service.DictService;
import com.qidian.mes.util.dot.PagerDTO;
import com.qidian.mes.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 戴着假发的程序员
 * @Company 起点编程
 * @Description
 * @Time 2024/4/2 11:20
 */
@Service("dictServiceImpl")
public class DictServiceImpl implements DictService {

    @Autowired
    private DictMapper dictMapper;

    @Override
    public QdResult deletes(Long[] dictIds) {
        dictMapper.deleteByIds(dictIds);
        return QdResult.success();
    }

    @Override
    public QdResult deleteDict(Long dictId) {
        dictMapper.deleteByDictIdInt(dictId);
        return  QdResult.success();
    }

    @Override
    public QdResult updateDict(Dict dict) {
        if(dict.getDictId() == null)
            return QdResult.error("dictId is null");
        // 校验是否重复
        Dict tempDict = dictMapper.selectByType(dict.getDictType());
        if(tempDict != null && !tempDict.getDictId().equals(dict.getDictId()))
            return QdResult.duplicate("duplicate: dictType");
        tempDict = dictMapper.selectByName(dict.getDictName());
        if(tempDict != null && !tempDict.getDictId().equals(dict.getDictId()))
            return QdResult.duplicate("duplicate: dictName");
        return QdResult.isSuccess(dictMapper.updateDict(dict));
    }

    @Override
    public QdResult queryById(Long dictId) {
        return QdResult.success(dictMapper.selectById(dictId));
    }

    @Override
    public QdResult saveDict(Dict dict) {
        // 校验是否重复
        if(dictMapper.selectByType(dict.getDictType()) != null)
            return QdResult.duplicate("duplicate: dictType");
        if (dictMapper.selectByName(dict.getDictName()) != null)
            return QdResult.duplicate("duplicate: dictName");
        //return dictMapper.insertDict(dict) == 1 ? QdResult.success() : QdResult.error();
        return QdResult.isSuccess(dictMapper.insertDict(dict));
    }

    @Override
    public QdResult<PagerDTO<Dict>> queryByPage(Dict sysDict, int page, int limit) {
        PagerDTO<Dict> pagerDTO = new PagerDTO<>();
        pagerDTO.setPage(page);
        pagerDTO.setLimit(limit);
        pagerDTO.setTotal(dictMapper.selectCount(sysDict));
        pagerDTO.setData(dictMapper.selectByPage(sysDict,(page - 1) * limit,limit));
        return QdResult.success(pagerDTO);
    }

    @Override
    public QdResult<List<Dict>> queryAll() {
        return QdResult.success(dictMapper.queryAll());
    }
}
